var utils = require("../../../utils/util.js");
var app = getApp();
Page({
  data: {
    dominameImg: utils.dominameImg,
    hasdefultAddress:false
  },
  onLoad: function (options){
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: utils.dominame+'/api/v1/goods/buyNowInfo',
      data: {
        goods_id: options.id,
        goods_cart_counts:options.counts,
        token: app.globalData.token
      },
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      complete: function (res) {
        setTimeout(function () {
          wx.hideLoading()
        }, 100)
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            actual:res.data.data.actual,
            art_no:res.data.data.art_no,
            freight_sum: res.data.data.freight_sum.toFixed(2),
            pic: res.data.data.pic,
            salePrice: res.data.data.sale_price,
            salePriceSum: res.data.data.sale_price_sum,
            counts: res.data.data.goods_cart_counts,
            shop_name: res.data.data.shop_name,
            shop_goods_id: res.data.data.shop_goods_id
          });
        }
      }
    })
  },
  submitOrder:function(){
    let that=this;
    if (this.data.hasdefultAddress){
      wx.request({
        url: utils.dominame + '/api/v1/goods/buyNow',
        data: {
          goods_id: that.data.shop_goods_id,
          goods_cart_counts: that.data.counts,
          token: app.globalData.token,
          user_address_id: that.data.address_id,
          platform:2
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        complete: function (res) {
          setTimeout(function () {
            wx.hideLoading()
          }, 100)
        },
        success: function (res) {
          console.log(res);
          if (res.data.code == 0) {
            wx.navigateTo({
              url: '../../payChoose/payChoose?user_balance=' + res.data.data.user_balance + '&actual=' + res.data.data.actual + '&order_id=' + res.data.data.order_id + '&order_no=' + res.data.data.order_no,
            });
          }
        }
      })
    }else{
      wx.showToast({
        title: '请添加收货地址',
        icon:'none'
      })
    }
  },
  goAddressList:function(){
    wx.navigateTo({
      url: '../../personCenter/addressList/addressList',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let dataPackage = {
      token: app.globalData.token
    };
    if (this.data.address_id){
      dataPackage.address_id = this.data.address_id
    };
    wx.request({
      url: utils.dominame + '/api/v1/order/getDefaultAddress',
      data: dataPackage,
      method: 'GET',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            hasdefultAddress: true,
            name:res.data.data.name,
            phone: res.data.data.phone,
            province_name: res.data.data.province_name,
            city_name: res.data.data.city_name,
            area_name: res.data.data.area_name,
            address: res.data.data.address,
            address_id: res.data.data.id
          });
        } else {
          that.setData({
            hasdefultAddress: false
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})